package calc;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class StandardView extends JPanel implements KeyListener {
	private static JTextArea txtNum;
	
	private JButton btnNumber9;
	private JButton btnNumber8;
	private JButton btnNumber7;
	private JButton btnNumber6;
	private JButton btnNumber5;
	private JButton btnNumber4;
	private JButton btnNumber3;
	private JButton btnNumber2;
	private JButton btnNumber1;
	private JButton btnNumber0;
	
	private JButton btnPlus;
	private JButton btnMinus;
	private JButton btnDivide;
	private JButton btnMultiply;
	private JButton btnEnter;
	private JButton btnClearScrn;
	private JButton btnDecimalpoint;
	private JButton btnBackspace;
	private JButton btnBracketOpen;
	private JButton btnBracketClose;
	private JButton btnPower;
	

	public StandardView(){		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 30, 30, 30, 30, 30};
		gridBagLayout.rowHeights = new int[]{40, 20, 20, 20, 20, 20};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
		setLayout(gridBagLayout);	
		
		txtNum = new JTextArea();
		txtNum.setFont(new Font("Tahoma", Font.PLAIN, 20));
		txtNum.setEditable(false);
		txtNum.addKeyListener(this);
		txtNum.requestFocusInWindow();
		GridBagConstraints gbc_txtNum = new GridBagConstraints();
		gbc_txtNum.insets = new Insets(5, 0, 5, 5);
		gbc_txtNum.fill = GridBagConstraints.BOTH;
		gbc_txtNum.gridx = 1;
		gbc_txtNum.gridy = 0;
		gbc_txtNum.gridwidth = 5;
		add(txtNum, gbc_txtNum);
			
		
		//======================== KEY PAD ========================
		btnNumber9 = new JButton("9");
		btnNumber9.setName("9");
		btnNumber9.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnNumber9 = new GridBagConstraints();
		gbc_btnNumber9.insets = new Insets(0, 0, 5, 5);
		gbc_btnNumber9.fill = GridBagConstraints.BOTH;
		gbc_btnNumber9.gridx = 3;
		gbc_btnNumber9.gridy = 2;
		add(btnNumber9, gbc_btnNumber9);
		
		btnNumber8 = new JButton("8");
		btnNumber8.setName("8");
		btnNumber8.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnNumber8 = new GridBagConstraints();
		gbc_btnNumber8.insets = new Insets(0, 0, 5, 5);
		gbc_btnNumber8.fill = GridBagConstraints.BOTH;
		gbc_btnNumber8.gridx = 2;
		gbc_btnNumber8.gridy = 2;
		add(btnNumber8, gbc_btnNumber8);
		
		btnNumber7 = new JButton("7");
		btnNumber7.setName("7");
		btnNumber7.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnNumber7 = new GridBagConstraints();
		gbc_btnNumber7.insets = new Insets(0, 0, 5, 5);
		gbc_btnNumber7.fill = GridBagConstraints.BOTH;
		gbc_btnNumber7.gridx = 1;
		gbc_btnNumber7.gridy = 2;
		add(btnNumber7, gbc_btnNumber7);
		
		btnNumber6 = new JButton("6");
		btnNumber6.setName("6");
		btnNumber6.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnNumber6 = new GridBagConstraints();
		gbc_btnNumber6.insets = new Insets(0, 0, 5, 5);
		gbc_btnNumber6.fill = GridBagConstraints.BOTH;
		gbc_btnNumber6.gridx = 3;
		gbc_btnNumber6.gridy = 3;
		add(btnNumber6, gbc_btnNumber6);
		
		btnNumber5 = new JButton("5");
		btnNumber5.setName("5");
		btnNumber5.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnNumber5 = new GridBagConstraints();
		gbc_btnNumber5.insets = new Insets(0, 0, 5, 5);
		gbc_btnNumber5.fill = GridBagConstraints.BOTH;
		gbc_btnNumber5.gridx = 2;
		gbc_btnNumber5.gridy = 3;
		add(btnNumber5, gbc_btnNumber5);
		
		btnNumber4 = new JButton("4");
		btnNumber4.setName("4");
		btnNumber4.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnNumber4 = new GridBagConstraints();
		gbc_btnNumber4.insets = new Insets(0, 0, 5, 5);
		gbc_btnNumber4.fill = GridBagConstraints.BOTH;
		gbc_btnNumber4.gridx = 1;
		gbc_btnNumber4.gridy = 3;
		add(btnNumber4, gbc_btnNumber4);
		
		btnNumber3 = new JButton("3");
		btnNumber3.setName("3");
		btnNumber3.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnNumber3 = new GridBagConstraints();
		gbc_btnNumber3.insets = new Insets(0, 0, 5, 5);
		gbc_btnNumber3.fill = GridBagConstraints.BOTH;
		gbc_btnNumber3.gridx = 3;
		gbc_btnNumber3.gridy = 4;
		add(btnNumber3, gbc_btnNumber3);
		
		btnNumber2 = new JButton("2");
		btnNumber2.setName("2");
		btnNumber2.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnNumber2 = new GridBagConstraints();
		gbc_btnNumber2.insets = new Insets(0, 0, 5, 5);
		gbc_btnNumber2.fill = GridBagConstraints.BOTH;
		gbc_btnNumber2.gridx = 2;
		gbc_btnNumber2.gridy = 4;
		add(btnNumber2, gbc_btnNumber2);
		
		btnNumber1 = new JButton("1");
		btnNumber1.setName("1");
		btnNumber1.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnNumber1 = new GridBagConstraints();
		gbc_btnNumber1.insets = new Insets(0, 0, 5, 5);
		gbc_btnNumber1.fill = GridBagConstraints.BOTH;
		gbc_btnNumber1.gridx = 1;
		gbc_btnNumber1.gridy = 4;
		add(btnNumber1, gbc_btnNumber1);
		
		btnNumber0 = new JButton("0");
		btnNumber0.setName("0");
		btnNumber0.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnNumber0 = new GridBagConstraints();
		gbc_btnNumber0.insets = new Insets(0, 0, 5, 5);
		gbc_btnNumber0.fill = GridBagConstraints.BOTH;
		gbc_btnNumber0.gridx = 1;
		gbc_btnNumber0.gridy = 5;
		gbc_btnNumber0.gridwidth = 2;
		add(btnNumber0, gbc_btnNumber0);
		
		
		//=============== Operations =======================
		btnPlus = new JButton("+");
		btnPlus.setName("+");
		btnPlus.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnPlus = new GridBagConstraints();
		gbc_btnPlus.insets = new Insets(0, 0, 5, 5);
		gbc_btnPlus.fill = GridBagConstraints.BOTH;
		gbc_btnPlus.gridx = 4;
		gbc_btnPlus.gridy = 5;
		gbc_btnPlus.gridheight = 2;
		add(btnPlus, gbc_btnPlus);
		
		btnDivide = new JButton("/");
		btnDivide.setName("/");
		btnDivide.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnDivide = new GridBagConstraints();
		gbc_btnDivide.insets = new Insets(0, 0, 5, 5);
		gbc_btnDivide.fill = GridBagConstraints.BOTH;
		gbc_btnDivide.gridx = 4;
		gbc_btnDivide.gridy = 2;
		add(btnDivide, gbc_btnDivide);
		
		btnMultiply = new JButton("*");
		btnMultiply.setName("*");
		btnMultiply.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnMultiply = new GridBagConstraints();
		gbc_btnMultiply.insets = new Insets(0, 0, 5, 5);
		gbc_btnMultiply.fill = GridBagConstraints.BOTH;
		gbc_btnMultiply.gridx = 4;
		gbc_btnMultiply.gridy = 3;
		add(btnMultiply, gbc_btnMultiply);
		
		btnMinus = new JButton("-");
		btnMinus.setName("-");
		btnMinus.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnMinus = new GridBagConstraints();
		gbc_btnMinus.insets = new Insets(0, 0, 5, 5);
		gbc_btnMinus.fill = GridBagConstraints.BOTH;
		gbc_btnMinus.gridx = 4;
		gbc_btnMinus.gridy = 4;
		add(btnMinus, gbc_btnMinus);		
		
		btnPower = new JButton("^");
		btnPower.setName("^");
		btnPower.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnPower = new GridBagConstraints();
		gbc_btnPower.insets = new Insets(0, 0, 5, 5);
		gbc_btnPower.fill = GridBagConstraints.BOTH;
		gbc_btnPower.gridx = 4;
		gbc_btnPower.gridy = 1;
		add(btnPower, gbc_btnPower);
		
		
		//=============== Other buttons =======================
		btnEnter = new JButton("=");
		btnEnter.addActionListener(new Enter());
		GridBagConstraints gbc_btnEnter = new GridBagConstraints();
		gbc_btnEnter.insets = new Insets(0, 0, 5, 5);
		gbc_btnEnter.fill = GridBagConstraints.BOTH;
		gbc_btnEnter.gridx = 5;
		gbc_btnEnter.gridy = 4;
		gbc_btnEnter.gridheight = 2;
		add(btnEnter, gbc_btnEnter);
				
		btnBracketOpen = new JButton("(");
		btnBracketOpen.setName("(");
		btnBracketOpen.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnBracketOpen = new GridBagConstraints();
		gbc_btnBracketOpen.insets = new Insets(0, 0, 5, 5);
		gbc_btnBracketOpen.fill = GridBagConstraints.BOTH;
		gbc_btnBracketOpen.gridx = 2;
		gbc_btnBracketOpen.gridy = 1;
		add(btnBracketOpen, gbc_btnBracketOpen);

		btnBracketClose = new JButton(")");
		btnBracketClose.setName(")");
		btnBracketClose.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnBracketClose = new GridBagConstraints();
		gbc_btnBracketClose.insets = new Insets(0, 0, 5, 5);
		gbc_btnBracketClose.fill = GridBagConstraints.BOTH;
		gbc_btnBracketClose.gridx = 3;
		gbc_btnBracketClose.gridy = 1;
		add(btnBracketClose, gbc_btnBracketClose);
		
		btnBackspace = new JButton("←");
		btnBackspace.addActionListener(new Backspace());
		GridBagConstraints gbc_btnBackspace = new GridBagConstraints();
		gbc_btnBackspace.insets = new Insets(0, 0, 5, 5);
		gbc_btnBackspace.fill = GridBagConstraints.BOTH;
		gbc_btnBackspace.gridx = 5;
		gbc_btnBackspace.gridy = 1;
		add(btnBackspace, gbc_btnBackspace);
		
		btnClearScrn = new JButton("C");
		btnClearScrn.addActionListener(new Clear());
		GridBagConstraints gbc_btnClearScrn = new GridBagConstraints();
		gbc_btnClearScrn.insets = new Insets(0, 0, 5, 5);
		gbc_btnClearScrn.fill = GridBagConstraints.BOTH;
		gbc_btnClearScrn.gridx = 1;
		gbc_btnClearScrn.gridy = 1;
		add(btnClearScrn, gbc_btnClearScrn);
		
		btnDecimalpoint = new JButton(".");
		btnDecimalpoint.setName(".");
		btnDecimalpoint.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnDecimalpoint = new GridBagConstraints();
		gbc_btnDecimalpoint.insets = new Insets(0, 0, 5, 5);
		gbc_btnDecimalpoint.fill = GridBagConstraints.BOTH;
		gbc_btnDecimalpoint.gridx = 3;
		gbc_btnDecimalpoint.gridy = 5;
		add(btnDecimalpoint, gbc_btnDecimalpoint);
	}
	
	public static String getInputText() {
		return txtNum.getText();
	}
	
	public static void setInputText(String answer) {
		txtNum.setText(answer);
	}
	
	public static void appendInputText(String answer) {
		txtNum.append(answer);
	}
	
	public static JTextArea getTxtNum(){
		return txtNum;
	}
	
	class Clear implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			txtNum.setText("");
		}
	}
	
	class Enter implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			try {
				txtNum.setText(InputParser.Calculate(txtNum.getText()));
			} catch(AshCalcException ex) {
				JOptionPane.showMessageDialog(NewCalc.frame, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	
	class Backspace implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			if(txtNum.getText().length() == 0) return;
			txtNum.setText(txtNum.getText().substring(0, txtNum.getText().length() -1));
		}
	}
	
	public static class Numbpad implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {	
			JButton o = (JButton) arg0.getSource();
			txtNum.append(o.getName());
			txtNum.requestFocusInWindow();
		}
	}
	
	public void keyPressed(KeyEvent arg0) {}
	public void keyTyped(KeyEvent arg0) {}
	public void keyReleased(KeyEvent arg0) {
		System.out.println("" + arg0.getKeyCode() + " " + arg0.getKeyChar());
		if(arg0.getKeyCode() == 127) btnClearScrn.doClick();
		if(arg0.getKeyCode() == 8) btnBackspace.doClick();
		if(arg0.getKeyCode() == 111) btnDivide.doClick();
		if(arg0.getKeyCode() == 106) btnMultiply.doClick();
		if(arg0.getKeyCode() == 109) btnMinus.doClick();
		if(arg0.getKeyCode() == 107) btnPlus.doClick();
		if(arg0.getKeyCode() == 110) btnDecimalpoint.doClick();
		if(arg0.getKeyCode() == 10) btnEnter.doClick();
		if(arg0.getKeyCode() == 96) btnNumber0.doClick();
		if(arg0.getKeyCode() == 97) btnNumber1.doClick();
		if(arg0.getKeyCode() == 98) btnNumber2.doClick();
		if(arg0.getKeyCode() == 99) btnNumber3.doClick();
		if(arg0.getKeyCode() == 100) btnNumber4.doClick();
		if(arg0.getKeyCode() == 101) btnNumber5.doClick();
		if(arg0.getKeyCode() == 102) btnNumber6.doClick();
		if(arg0.getKeyCode() == 103) btnNumber7.doClick();
		if(arg0.getKeyCode() == 104) btnNumber8.doClick();
		if(arg0.getKeyCode() == 105) btnNumber9.doClick();
		if(arg0.getKeyChar() == '^') btnPower.doClick();
		if(arg0.getKeyChar() == '(') btnBracketOpen.doClick();
		if(arg0.getKeyChar() == ')') btnBracketClose.doClick();
	}
}
